﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;

namespace ChangDiTools {
    public class Texts {
        [CommandMethod("AddText")]
        public void AddText() {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction()) {
                DBText textFirst = new DBText(); // 创建第一个单行文字
                textFirst.Position = new Point3d(50, 50, 0);//文字位置
                textFirst.Height = 5;//文字高度 
                //设置文字内容，特殊格式为≈、下划线和平方
                textFirst.TextString = "面积" + TextSpecialSymbol.AlmostEqual + TextSpecialSymbol.Underline + "2000" + TextSpecialSymbol.Underline + "m" + TextSpecialSymbol.Square;
                //设置文字的水平对齐方式为居中
                textFirst.HorizontalMode = TextHorizontalMode.TextCenter;
                //设置文字的垂直对齐方式为居中
                textFirst.VerticalMode = TextVerticalMode.TextBottom;
                //设置文字的对齐点
                textFirst.AlignmentPoint = textFirst.Position;
                DBText textSecond = new DBText();// 创建第二个单行文字
                textSecond.Height = 5; //文字高度
                //设置文字内容，特殊格式为角度、希腊字母和度数
                textSecond.TextString = TextSpecialSymbol.Angle + TextSpecialSymbol.Belta + "=45" + TextSpecialSymbol.Degree;
                //设置文字的对齐方式为居中对齐
                textSecond.HorizontalMode = TextHorizontalMode.TextCenter;
                textSecond.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置文字的对齐点
                textSecond.AlignmentPoint = new Point3d(50, 40, 0);
                DBText textLast = new DBText();//创建第三个单行文字
                textLast.Height = 5;// 文字高度
                //设置文字的内容，特殊格式为直径和公差
                textLast.TextString = TextSpecialSymbol.Diameter + "30的直径偏差为" + TextSpecialSymbol.Tolerance + "0.01";
                //设置文字的对齐方式为居中对齐
                textLast.HorizontalMode = TextHorizontalMode.TextCenter;
                textLast.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置文字的对齐点
                textLast.AlignmentPoint = new Point3d(50, 30, 0);
                db.AddEntity(textFirst, textSecond, textLast);//添加文本到模型空间
                trans.Commit();//提交事务处理
            }
        }

        [CommandMethod("AddStackText")]
        public void AddStackText() {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction()) {
                MText mtext = new MText();//创建多行文本对象
                mtext.Location = new Point3d(100, 40, 0);//位置
                //创建水平分数形式的堆叠文字
                string firstLine = TextTools.StackText(TextSpecialSymbol.Diameter + "20", "H7", "P7", StackType.HorizontalFraction, 0.5);
                //创建斜分数形式的堆叠文字
                string secondLine = TextTools.StackText(TextSpecialSymbol.Diameter + "20", "H7", "P7", StackType.ItalicFraction, 0.5);
                //创建公差形式的堆叠文字
                string lastLine = TextTools.StackText(TextSpecialSymbol.Diameter + "20", "+0.020", "-0.010", StackType.Tolerance, 0.5);
                //将前面定义的堆叠文字合并，作为多行文本的内容
                mtext.Contents = firstLine + MText.ParagraphBreak + secondLine + "\n" + lastLine;
                mtext.TextHeight = 5;//文本高度
                mtext.Width = 0;//文本宽度，设为0表示不会自动换行
                //设置多行文字的对齐方式正中
                mtext.Attachment = AttachmentPoint.MiddleCenter;
                db.AddEntity(mtext);//添加文本到模型空间中
                trans.Commit();//提交事务处理
            }
        }
        [CommandMethod("DimTest")]
        public void DimTest()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction()) {
                // 创建要标注的图形
                Line line1 = new Line(new Point3d(30, 20, 0), new Point3d(120, 20, 0));
                Line line2 = new Line(new Point3d(120, 20, 0), new Point3d(120, 40, 0));
                Line line3 = new Line(new Point3d(120, 40, 0), new Point3d(90, 80, 0));
                Line line4 = new Line(new Point3d(90, 80, 0), new Point3d(30, 80, 0));
                Arc arc = new Arc(new Point3d(30, 50, 0), 30, Math.PI / 2, Math.PI * 3 / 2);
                Circle cir1 = new Circle(new Point3d(30, 50, 0), Vector3d.ZAxis, 15);
                Circle cir2 = new Circle(new Point3d(70, 50, 0), Vector3d.ZAxis, 10);
                db.AddEntity(line1, line2, line3, line4, arc, cir1, cir2);
                List<Dimension> dims=new List<Dimension>();
                RotatedDimension dimRotated1=new RotatedDimension();
                dimRotated1.XLine1Point = line1.StartPoint;
                dimRotated1.XLine2Point = line1.EndPoint;
                dimRotated1.DimLinePoint = GeTools.MidPoints(line1).PolarPointsl(-Math.PI / 2, 10);
                dimRotated1.DimensionText = "<>mm";
                dims.Add(dimRotated1);

                RotatedDimension dimRotated2 = new RotatedDimension();
                dimRotated2.Rotation = Math.PI / 2;//转角标注角度为90度，表示垂直方向
                //指定两条尺寸界线的附着位置和尺寸线的位置
                dimRotated2.XLine1Point = line2.StartPoint;
                dimRotated2.XLine2Point = line2.EndPoint;
                dimRotated2.DimLinePoint = GeTools.MidPoints(line2.StartPoint, line2.EndPoint).PolarPointsl(0, 10);
                dims.Add(dimRotated2);//将垂直转角标注添加到列表中
                //创建转角标注（尺寸公差标注）
                RotatedDimension dimRotated3 = new RotatedDimension();
                //指定两条尺寸界线的附着位置和尺寸线的位置
                dimRotated3.XLine1Point = line4.StartPoint;
                dimRotated3.XLine2Point = line4.EndPoint;
                dimRotated3.DimLinePoint = GeTools.MidPoints(line4.StartPoint, line4.EndPoint).PolarPointsl(Math.PI / 2, 10);
                //设置标注的文字为标注值+堆叠文字
                dimRotated3.DimensionText = TextTools.StackText("<>", "+0.026", "-0.025", StackType.Tolerance, 0.7);
                dims.Add(dimRotated3);//将尺寸公差标注添加到列表中
                // 创建对齐标注
                AlignedDimension dimAligned = new AlignedDimension();
                //指定两条尺寸界线的附着位置和尺寸线的位置
                dimAligned.XLine1Point = line3.StartPoint;
                dimAligned.XLine2Point = line3.EndPoint;
                dimAligned.DimLinePoint = GeTools.MidPoints(line3.StartPoint, line3.EndPoint).PolarPointsl(Math.PI / 2, 10);
                //设置标注的文字为标注值+公差符号
                dimAligned.DimensionText = "<>" + TextSpecialSymbol.Tolerance + "0.2";
                dims.Add(dimAligned);//将对齐标注添加到列表中
                // 创建半径标注
                RadialDimension dimRadial = new RadialDimension();
                dimRadial.Center = cir1.Center;//圆或圆弧的圆心
                //用于附着引线的圆或圆弧上的点
                dimRadial.ChordPoint = cir1.Center.PolarPointsl(GeTools.DegreeToRadian(30), 15);
                dimRadial.LeaderLength = 10;//引线长度
                dims.Add(dimRadial);//将半径标注添加到列表中
                // 创建直径标注
                DiametricDimension dimDiametric = new DiametricDimension();
                //圆或圆弧上第一个直径点的坐标
                dimDiametric.ChordPoint = cir2.Center.PolarPointsl(GeTools.DegreeToRadian(45), 10);
                //圆或圆弧上第二个直径点的坐标
                dimDiametric.FarChordPoint = cir2.Center.PolarPointsl(GeTools.DegreeToRadian(-135), 10);
                dimDiametric.LeaderLength = 0;//从 ChordPoint 到注解文字或折线处的长度
                dims.Add(dimDiametric);//将直径标注添加到列表中
                // 创建角度标注
                Point3AngularDimension dimLineAngular = new Point3AngularDimension();
                //圆或圆弧的圆心、或两尺寸界线间的共有顶点的坐标
                dimLineAngular.CenterPoint = line2.StartPoint;
                //指定两条尺寸界线的附着位置
                dimLineAngular.XLine1Point = line1.StartPoint;
                dimLineAngular.XLine2Point = line2.EndPoint;
                //设置角度标志圆弧线上的点
                dimLineAngular.ArcPoint = line2.StartPoint.PolarPointsl(GeTools.DegreeToRadian(135), 10);
                dims.Add(dimLineAngular);//将角度标注添加到列表中
                // 创建弧长标注,标注文字取为默认值
                ArcDimension dimArc = new ArcDimension(arc.Center, arc.StartPoint, arc.EndPoint, arc.Center.PolarPointsl(Math.PI, arc.Radius + 10), "<>", db.Dimstyle);
                dims.Add(dimArc);//将弧长标注添加到列表中
                // 创建显示X轴值的坐标标注
                OrdinateDimension dimX = new OrdinateDimension();
                dimX.UsingXAxis = true;//显示 X 轴值
                dimX.DefiningPoint = cir2.Center;//标注点
                //指定引线终点，即标注文字显示的位置
                dimX.LeaderEndPoint = cir2.Center.PolarPointsl(-Math.PI / 2, 20);
                dims.Add(dimX);//将坐标标注添加到列表中
                // 创建显示Y轴值的坐标标注
                OrdinateDimension dimY = new OrdinateDimension();
                dimY.UsingXAxis = false;//显示Y轴                
                dimY.DefiningPoint = cir2.Center;//标注点
                //指定引线终点，即标注文字显示的位置
                dimY.LeaderEndPoint = cir2.Center.PolarPointsl(0, 20);
                dims.Add(dimY);//将坐标标注添加到列表中
                foreach (Dimension dim in dims)//遍历标注列表
                {
                    dim.DimensionStyle = db.Dimstyle;//设置标注样式为当前样式
                    db.AddEntity(dim);//将标注添加到模型空间中
                }
                trans.Commit();//提交更改
            }
        }
    }
}